/**
 * 
 */
package com.example.ad_project;

import java.util.HashMap;

import org.json.JSONException;

import android.os.Bundle;
import android.os.StrictMode;
import android.os.StrictMode.ThreadPolicy;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.nus.iss.ad.dbhelper.RetrivalListDBHelper;
import com.nus.iss.ad.model.DepartmentInventoryDetailsModel;
import com.nus.iss.ad.model.RetrivalDetailsModel;

/**
 * @author Sachin
 * 
 */
public class RetrivalDetailsActivity extends BaseActivity {

	String selectedId = null;
	TextView itemName, totalQty;
	// Button saveButton;
	ListView lv;

	static HashMap<String, String> valueSet = new HashMap<String, String>();

	RetrivalDetailsModel r = null;
	RetrivalListDBHelper data = new RetrivalListDBHelper();

	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		StrictMode.setThreadPolicy(ThreadPolicy.LAX);

		Bundle extras = getIntent().getExtras();
		// to get the extra from the call
		if (extras != null) {
			selectedId = extras.getString("selectedId");
			
		}

		setContentView(R.layout.retrival_details_layout_activity);

		try {
			r = data.getRetrivalDetail(selectedId);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		/*
		 * List<User> userList = new ArrayList<User>();
		 * 
		 * userList.add(new User("Sachin", "10")); userList.add(new
		 * User("Sachin", "10")); userList.add(new User("Sachin", "10"));
		 * userList.add(new User("Sachin", "10")); userList.add(new
		 * User("Sachin", "10"));
		 */

		itemName = (TextView) findViewById(R.id.itemNameTV);
		totalQty = (TextView) findViewById(R.id.totalQtyTV);

		itemName.setText(r.getItemName());

		totalQty.setText(r.getTotalQuantity() + "");

		lv = (ListView) findViewById(R.id.deptInvDetails);
		// lv.setAdapter(new SimpleAdapter(this, r.getDeptDetailList(),
		// R.layout.retrival_details_row_layout,
		// new String[] { "deptName", "qtyExpected", "qtyRetrived"}, new int[] {
		// R.id.deptName, R.id.expectedQtyET , R.id.requiredQtyTV}));

		DepartmentInventoryDetailsModel[] array = new DepartmentInventoryDetailsModel[r
				.getDeptDetailList().size()];
		lv.setAdapter(new RetrivalDetailsAdapter(this, r.getDeptDetailList()
				.toArray(array)));
		// lv.setAdapter(new EditorAdapter(this, r.getDeptDetailList()));

		// lv.setOnItemClickListener(this);

	}

	public void onSaveClick(View view) {

		// ArrayList<View> outViews=new ArrayList<View>();
		// lv.findViewsWithText(outViews,"Confirm",ListView.FIND_VIEWS_WITH_TEXT);

		// for(DepartmentInventoryDetailsModel d : r.getDeptDetailList()){'

		boolean checkFlag = false;

		int totalValue = 0;

		for (int i = 0; i < r.getDeptDetailList().size(); i++) {
			// View view2= lv.getChildAt(i);

			DepartmentInventoryDetailsModel dm = (DepartmentInventoryDetailsModel) lv
					.getAdapter().getItem(i);

			totalValue = totalValue + dm.getQtyExpected();

			// RetrivalListActivity.rlist.get(i).setRequired(dm.getQtyExpected());

			if (dm.isCheckFlag()) {

				checkFlag = dm.isCheckFlag();

			}

		}

		if (checkFlag) {
			Toast.makeText(getApplicationContext(), "Please check the values",
					Toast.LENGTH_LONG).show();
		} else {
			// save logic....

			// Intent i = new Intent(this, RetrivalDetailsActivity.class);
			// startActivity(i);

			valueSet = new HashMap<String, String>();
			valueSet.put("selectedId", selectedId);
			valueSet.put("value", totalValue + "");

			
			
			
			for(int i =0; i<r.getDeptDetailList().size(); i++){
				
				DepartmentInventoryDetailsModel dm = (DepartmentInventoryDetailsModel) lv
						.getAdapter().getItem(i);
				
				try {
					data.updateJson(i, dm.getQtyExpected(),selectedId);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
			}
			
			super.onBackPressed();
			
		}

	}

}
